package cn.jaye.fabric.common.listener;

import cn.jaye.fabric.common.constants.Global;
import cn.jaye.fabric.common.model.SystemSession;
import cn.jaye.fabric.domain.model.User;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

/**
 * @author Jaye
 * @create 2018-05-28 17:24
 */
@Component
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        if (Global.SESSION_USER_KEY.equals(httpSessionBindingEvent.getName())) {
            SystemSession.setUser((User) httpSessionBindingEvent.getValue());
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        if (Global.SESSION_USER_KEY.equals(httpSessionBindingEvent.getName())) {
            SystemSession.setUser((User) httpSessionBindingEvent.getValue());
        }
    }
}
